package com.web;

import com.domain.User;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 客户报名活动
 * 接收addActPlanSelf.jsp页面的phone、password、activityId数据
 * 利用客户的phone、password数据进行查询客户id
 * 提交到userService的addActPlan函数进行添加
 * 返回到/ShowUserSelf
 */
@WebServlet("/addActPlanSelf")
public class addActPlanSelfServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.接收参数
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String activityId = request.getParameter("activityId");

        //3.进行添加
        userService userService = new userService();
        //3.1 查询用户的id
        User user = userService.loginUser(phone, password);
        //3.2 进行添加 用用户的id和活动的id进行添加actPlan表
        Integer integer = userService.addActPlan(user.getUserId(), Integer.parseInt(activityId));
        System.out.println(integer==1?"客户报名成功":"客户报名失败");

        //4.进行重定向到客户的主页面
        response.sendRedirect(request.getContextPath()+"/ShowUserSelf?userId="+user.getUserId());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
